package com.zlll.winner.service.lhtUser;

import com.zlll.winner.business.model.lhtUser.LhtUser;
import com.zlll.winner.business.model.lhtUser.LhtUserIntegralDetail;
import com.zlll.winner.business.model.lhtUser.LhtUserIntegralTask;
import com.zlll.winner.business.service.lhtUser.ILhtUserIntegralTaskService;
import com.zlll.winner.business.service.lhtUser.ILhtUserService;
import com.zlll.winner.mapper.lhtUser.LhtUserIntegralDetailMapper;
import com.zlll.winner.business.service.lhtUser.ILhtUserIntegralDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlll.winner.mapper.lhtUser.LhtUserIntegralTaskMapper;
import com.zlll.winner.mapper.lhtUser.LhtUserMapper;
import com.zlll.winner.service.lhtUser.async.LhtUserAsyncService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户积分明细表
 * 服务实现类
 * </p>
 *
 * @author qsx
 * @since 2019-09-30
 */
@Service
public class LhtUserIntegralDetailServiceImpl extends ServiceImpl<LhtUserIntegralDetailMapper, LhtUserIntegralDetail> implements ILhtUserIntegralDetailService {

    @Autowired
    private LhtUserIntegralDetailMapper integralDetailMapper;

    @Autowired
    private ILhtUserIntegralTaskService taskService;

    @Autowired
    private LhtUserAsyncService lhtUserAsyncService;

    @Autowired
    private ILhtUserService userService;
    @Override
    public Boolean isAdd(Long userId, Long taskId) {
        List<Map<String, Object>> list = integralDetailMapper.getTodayDailyTasksList(userId, taskId);
        if (list.size() > 0) {
            Map<String, Object> map = list.get(0);
            Integer countNum = Integer.parseInt(map.get("countNum").toString());
            Integer num = Integer.parseInt(map.get("num").toString());
            if (countNum >= num) {
                return false;
            }
            return true;
        }
        return true;
    }

    @Override
    public Boolean saveDailyTasks(LhtUserIntegralDetail integralDetail) throws InterruptedException {
        Long time = System.currentTimeMillis();

//        LhtUserIntegralTask integralTask = integralTaskMapper.selectById();
        LhtUserIntegralTask integralTask =  taskService.getById(integralDetail.getTaskId());
        //        LhtUser user = userMapper.selectById(integralDetail.getUserId());
//        user.setTotalIntegral(integralTask.getIntegralNum() + user.getTotalIntegral());
//        user.setResidueIntegral(user.getResidueIntegral() + integralTask.getIntegralNum());
//        userMapper.updateById(user);

        integralDetail.setCreateTime(time);
        Boolean result = save(integralDetail);
        if (result){
            lhtUserAsyncService.changeIntegral(integralDetail.getUserId(), integralTask.getIntegralNum());

        }
        return result;
    }

    @Override
    public Boolean saveWatchTasks(LhtUserIntegralDetail integralDetail) throws InterruptedException {
        Long time = System.currentTimeMillis();
        Integer integralNum = 0;
        List<Map<String, Object>> list = integralDetailMapper.getTodayWatchTasksList(integralDetail.getUserId());
        if (list.size() > 0) {
            //获取当日累计时长
            Integer sumNum = Integer.parseInt(list.get(0).get("sumNum").toString());
            //获取当日累计积分
            Integer sumIntegralNum = Integer.parseInt(list.get(0).get("sumIntegralNum").toString());
            if (sumNum < 60) {
                integralNum = getIntegral(integralDetail.getAccumulateTime() + sumNum, sumIntegralNum);
            }

        } else {
            integralNum = getIntegral(integralDetail.getAccumulateTime(), 0);
        }
        integralDetail.setCreateTime(time);
        integralDetail.setIntegralNum(integralNum);
        integralDetailMapper.insert(integralDetail);

        if (integralNum > 0) {
            lhtUserAsyncService.changeIntegral(integralDetail.getUserId(), integralNum);
        }
        return true;
    }

    @Override
    public Object getTodayIntegrail(Long userId) {
        Map result = new HashMap();

        List<Map<String, Object>> list = integralDetailMapper.getTodayWatchTasksList(userId);
        if (list.size() > 0) {
            Map watchTasks = new HashMap();
            watchTasks.put("totalAccumulateTime",list.get(0).get("sumNum"));
            watchTasks.put("totalIntegralNum",list.get(0).get("sumIntegralNum"));

            result.put("watchTasks",watchTasks);
        }else{
            Map watchTasks = new HashMap();
            watchTasks.put("totalAccumulateTime",0);
            watchTasks.put("totalIntegralNum",0);
            result.put("watchTasks",watchTasks);

        }

        List<Map<String, Object>> dailyTasks = integralDetailMapper.getTodayDailyTasksList(userId, null);
        result.put("dailyTasks",dailyTasks);
        LhtUser user = userService.getById(userId);
        result.put("residueIntegral",user.getResidueIntegral());
        return result;
    }

    /**
     * 获取本次的积分
     *
     * @param time
     * @param sumIntegralNum
     * @return
     */
    public Integer getIntegral(Integer time, Integer sumIntegralNum) {
        Integer integral = 0;
        if (time >= 10 && time < 20) {
            integral = 10 - sumIntegralNum;
        } else if (time >= 20 && time < 30) {
            integral = 30 - sumIntegralNum;
        } else if (time >= 30 && time < 40) {
            integral = 60 - sumIntegralNum;
        } else if (time >= 40 && time < 50) {
            integral = 120 - sumIntegralNum;
        } else if (time >= 50 && time < 60) {
            integral = 200 - sumIntegralNum;
        } else if (time >= 60) {
            integral = 300 - sumIntegralNum;
        }
        return integral;
    }
}
